package com.mirabelle.juice.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

public class UserDetailsImpl implements UserDetails, Serializable {
	private static final long		serialVersionUID	= 1L;
	private String					login			= null;
	private String 					password 		= null;

	private List<GrantedAuthority>	authorities		= new ArrayList<GrantedAuthority>();

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}
	
	public void addRole(String role) {
		if (!authorities.contains(new GrantedAuthorityImpl(role))) {
			authorities.add(new GrantedAuthorityImpl(role));
		}
	}
	
	public void setUsername(String login) {
		this.login = login;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return login;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
	
	@Override
	public String toString() {
		String r = "login=" + login + " password=" + password + " [effectives roles] ";
		for (GrantedAuthority o : authorities) {
			r += o.getAuthority() + " ";
		}
		return r;
	}

}
